/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package factory;

import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author wida46984148
 */
public class PGame extends BasicGameState {

    Timer timer;
    public static final int ID = 0;
    Image imatgeFons;
    StateBasedGame state;
    ArrayList<NauBase> anb = null;
    aux a = new aux();
    //float x = 350; en original 800x600
    float x = 150;
    float y = 800;
    float zxc = -1300;
    int nivel;
    float enemigolvl;
    //boolean gamestart = false;
    boolean gameend = false;
    boolean gamepause = true;
    NauBase nbW = null;
    NauBase nbE = null;
    Shoot ms = null;
    Shoot msE = null;
    Shoot ms2 = null;
    int id = 0;
    int cnt = 0;
    boolean enemigo = true;

    public void init(GameContainer container, StateBasedGame game) throws SlickException {
        this.state = game;
        imatgeFons = new Image("src/data/superLand.png");
        nivel = 3;
        switch (nivel) {
            case 1:
                nbW = Naves.nausWida("nave2");
                a.posicionaUnitatAmiga(x, y, nbW);
                enemigolvl = 0.4f;
                break;
            case 2:
                enemigolvl = 0.5f;
                break;
            case 3:
                nbW = Naves.nausWida("nave3");
                a.posicionaUnitatAmiga(x, y, nbW);
                enemigolvl = 0.6f;
                break;
        }

    }

    public void update(GameContainer gc, StateBasedGame game, int delta) throws SlickException {

        Input input = gc.getInput();
        if (y > 480) {
            y -= 5;
            nbW.setLocation(x, y);
        }
        if (gameend) {
            if (y > -150) {
                y -= 5;
                nbW.setLocation(x, y);
            } else {
                game.enterState(2);
            }
        } else {
            if (y > 480) {
                y -= 5;
                nbW.setLocation(x, y);
            }
            if (input.isKeyDown(Input.KEY_A)) {
                float auxX = x;
                x += -enemigolvl * delta;
                if (x <= -30) {
                    x = auxX;
                }
                nbW.setLocation(x, y);
            }

            if (input.isKeyDown(Input.KEY_D)) {
                float auxX = x;
                x += enemigolvl * delta;
//                if (700 <= x) { en original 800X600
                if (300 <= x) {
                    x = auxX;
                }
                nbW.setLocation(x, y);
            }
            if (input.isKeyDown(Input.KEY_S)) {
                float hip = enemigolvl * delta;
                float auxY = y;
                x += hip * java.lang.Math.sin(java.lang.Math.toRadians(0.0));
                y += hip * java.lang.Math.cos(java.lang.Math.toRadians(0.0));
                if (480 <= y) {
                    y = auxY;
                }
                nbW.setLocation(x, y);
            }

            if (input.isKeyDown(Input.KEY_W)) {
                float hip = enemigolvl * delta;
                float auxY = y;
                x += hip * java.lang.Math.sin(java.lang.Math.toRadians(0.0));
                y -= hip * java.lang.Math.cos(java.lang.Math.toRadians(0.0));
                if (y <= 0) {
                    y = auxY;
                }
                nbW.setLocation(x, y);
            }

            if (input.isKeyPressed(Input.KEY_ENTER)) {
                enemigo = false;
                nbE = Naves.nausEnemigues("nave0");
                a.posicionaUnitatEnemiga(nbE);
//                msE = nbE.shoot.copiarShoot();
//                a.posShootE(nbE.getPosX(), nbE.getPosY(), (shootE) msE);
                //a.posShootE(nbE.getPosX()+58, nbE.getPosY()+100, (shootE) msE);
//                int i = 0;
//                timer = new Timer(300, new ActionListener() {
//
//                    public void actionPerformed(ActionEvent e) {
//                        msE = nbE.shoot.copiarShoot();
//                        a.posShootE(nbE.getPosX() + 58, nbE.getPosY() + 100, (shootE) msE);
//                        if (600 <= nbE.getPosY()) {
//                            timer.stop();
//                            enemigo = true;
//                        }
//                    }
//                });
//                timer.start();

            }
            if (input.isKeyPressed(Input.KEY_SPACE)) {

                if (nivel == 1) {
                    ms = nbW.shoot.copiarShoot();
                } else if (nivel == 3) {
                    ms = nbW.shoot.copiarShoot();
                }

//                ms = new MoveShoot(10, new Image("src/data/shot.jpg"));
//                a.posShoot(x + 65, y, ms);
                a.posShoot(x + 58, y, (MoveShoot) ms);
//                ms2 = new MoveShoot(10, new Image("src/data/shot.jpg"));
//                a.posShoot(x + 51, y, ms2);


            }

            if (input.isKeyPressed(Input.KEY_ESCAPE)) {
                gameend = true;
            }
        }
        a.update(delta);
        //zxc += 0.2;
        zxc = zxc != 0 ? zxc += 0.3 : 0;
    }

    public void render(GameContainer gc, StateBasedGame game, Graphics g)
            throws SlickException {
        imatgeFons.draw(0, zxc);
        a.dibuixar(g, gc);
//        p.dibuixar(g, gc);
//        mi.render(gc, g);
    }

//    @Override
//    public void enter(GameContainer gc, StateBasedGame state) {
//        p = new Tauler(6, 10, 1024, 600);
//        md = new ManagerDinersAures();
//        mi = new MenuIngame(gc, 0, 600, ManagerRecursos.getImage("contenidorIngameImage"), ManagerPerfil.getUnitatsTriades(), md, state);
//        mi.clear();
//        mi.reiniciarBotons();
//        ManagerContext.setDiners(md);
//        ManagerEnemics.iniciarCompteEnrere();
//    }
    @Override
    public int getID() {
        return ID;
    }
}
